<template>
  <views class="company_add">
    <!-- 企业基本信息 -->
    <div class="enterprise_info">
      <viewTitle :title="'企业基本信息'"></viewTitle>
      <div class="enterprise_info_flex">
        <el-form ref="form" label-width="auto">
          <el-form-item label="企业名称:">
            <el-input type="text" clearable placeholder="请输入关键字" v-model="company_name" ref="company_name" ></el-input>
          </el-form-item>
          <el-form-item label="统一社会信用代码:">
            <el-input type="text" clearable placeholder="请输入统一社会信用代码" v-model="organization_code" ref="organization_code" ></el-input>
          </el-form-item>
          <el-form-item label="经营地址:">
            <el-input type="textarea" clearable placeholder="请输入经营地址" v-model="company_address" ref="company_address" ></el-input>
          </el-form-item>
          <el-form-item label="经营期限:">
            <el-date-picker
              ref="deadline"
              v-model="deadline"
              value-format="timestamp"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="法人名称:">
            <el-input type="text" clearable placeholder="请输入法人名称" v-model="legal_person" ref="legal_person" ></el-input>
          </el-form-item>
          <el-form-item label="联系方式:">
            <el-input type="text" clearable placeholder="请输入联系方式" v-model="legal_phone" ref="legal_phone" ></el-input>
          </el-form-item>
          <el-form-item label="法人身份证号:">
            <el-input type="text" clearable placeholder="请输入法人身份证号" v-model="legal_card_no" ref="legal_card_no" ></el-input>
          </el-form-item>
          <el-form-item label="企业类型:">
            <el-select v-model="company_type" placeholder="企业类型">
              <el-option
                v-for="item in company_type_select"
                :key="item.type"
                :label="item.type_name"
                :value="item.type"
              ></el-option>
            </el-select>
          </el-form-item>
           <el-form-item label="快递费:">
            <el-input type="text" clearable placeholder="请输入快递费" v-model="postfee" ref="postfee" ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- 企业业务信息 -->
    <div class="enterprise_business">
      <viewTitle :title="'企业业务信息'"></viewTitle>
      <div class="enterprise_business_flex">
        <el-form ref="form" label-width="auto">
          <el-form-item label="负责人:">
            <el-input type="text" clearable placeholder="请输入负责人" v-model="control_man"></el-input>
          </el-form-item>
          <el-form-item label="联系方式:">
            <el-input type="text" clearable placeholder="请输入负责人联系方式" v-model="control_phone"></el-input>
          </el-form-item>
          <el-form-item label="物流线路:">
            <div
              class="flex flexR flexJ center"
              v-for="(item,index) in circuit_list"
              v-bind:key="index"
            >
              <div class="flex flexR">
                <div>
                  邵东
                </div>
                <div>-</div>
                <div>
                    <el-cascader
                    v-model="item.arrive_region_id"
                    :options="arrive_region"
                    :props="{ checkStrictly: true }"
                    clearable>
                    </el-cascader>
                </div>
              </div>
              <div>
                <el-button-group>
                  <el-button icon="el-icon-minus" @click="increase(index)"></el-button>
                  <el-button icon="el-icon-plus" @click="decrease(index)"></el-button>
                </el-button-group>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="统一社会信用代码:" :style="{height:'0px',overflow:'hidden'}">
            <el-input type="text" clearable placeholder="请输入统一社会信用代码" v-model="credit_code"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 附件上传 -->
    <div class="accessory">
      <viewTitle :title="'附件上传'"></viewTitle>
      <div class="accessory_flex">
        <el-form ref="form" label-width="auto" :inline="true">
          <el-form-item label="营业执照(副本):">
            <el-upload
              class="upload-demo"
              drag
              action="https://jsonplaceholder.typicode.com/posts/"
              :httpRequest="uploadLicenseImg"
              :show-file-list="false"
            >
              <img
                v-if="license_img"
                style="width:100%;height:100%"
                :src="license_img"
                v-default-img
                alt
              />
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                营业执照(副本)
                <em>点击上传</em>
              </div>
            </el-upload>
          </el-form-item>

          <el-form-item label="法人身份证:">
            <div class="flex flexR">
              <el-upload
                class="upload-demo"
                drag
                action="https://jsonplaceholder.typicode.com/posts/"
                :httpRequest="uploadLegalCardCons"
                :show-file-list="false"
              >
                <img
                  v-if="legal_card_cons"
                  style="width:100%;height:100%"
                  :src="legal_card_cons"
                  v-default-img
                  alt
                />
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                  身份证-反面
                  <em>点击上传</em>
                </div>
              </el-upload>
              <div class="cardL">
                <el-upload
                  class="upload-demo"
                  drag
                  action="https://jsonplaceholder.typicode.com/posts/"
                  :httpRequest="uploadLegalCardPros"
                  :show-file-list="false"
                >
                  <img
                    v-if="legal_card_pros"
                    style="width:100%;height:100%"
                    :src="legal_card_pros"
                    v-default-img
                    alt
                  />
                  <i class="el-icon-upload"></i>
                  <div class="el-upload__text">
                    身份证-正面
                    <em>点击上传</em>
                  </div>
                </el-upload>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <!-- 新增 （提交、返回） -->
      <div class="btn-list flex flexR center flexJp">
              <el-button type="primary" v-if="look!=1" @click="submitInfo">提交</el-button>
              <el-button type="" @click="returnRoute">返回</el-button>
      </div>
    </div>
  </views>
</template>

<script>
import  {mapGetters}  from 'vuex'

export default {
  data() {
    return {
      // ----------- 企业基本信息 ----------- //
      company_id: 0,    //企业ID
      company_name: "", // 企业名称
      organization_code: "", // 社会信用代码
      company_address: "", // 经营地址
      deadline: [], // 经营期限
      buiss_time_start: '',     //经营开始时间
      buiss_time_end: '',       //经营结束时间
      legal_person: "", // 法人名称
      legal_phone: "", // 法人联系方式
      legal_card_no: "", // 身份证号
      // ----------- 企业业务信息 ------------ //
      control_man: "", // 负责人
      control_phone: "", // 联系方式
      circuit_list: [], // 线路
      options: [],
      license_img: '',      //营业执照图片
      legal_card_pros: '',      //身份证正面
      legal_card_cons: '',      //身份证反面
      credit_code:"",
      arrive_region : [],       //所有到达站点
      company_type_select:[{type: 0, type_name: '物流'}, {type: 1, type_name: '快递'}],       //可选择的企业类型列表
      company_type:0,        //企业类型
	    postfee: ''
    };
  },
  computed:{
    ...mapGetters(['uploadFile']),
    postData(){
      let post_data = {};
      if (this.deadline && this.deadline.length > 0) {
        post_data.buiss_time_start =  this.deadline[0]/1000    //去掉后面的毫秒级
        post_data.buiss_time_end = this.deadline[1]/1000
      }else{
        return  {flag:false, ref: 'deadline', msg: '请选择经营时间'};
      }

      if(!this.deadline)    return  {flag:false, ref: 'deadline', msg: '请选择经营时间'}; 
      if(!this.company_name)    return  {flag:false, ref: 'company_name', msg: '请输入企业名称'}; 
      if(!this.organization_code)    return  {flag:false, ref: 'organization_code', msg: '请输入统一社会信用代码'}; 
      if(!this.company_address)    return  {flag:false, ref: 'company_address', msg: '请输入经营地址'}; 
      if(!this.legal_person)    return  {flag:false, ref: 'legal_person', msg: '请输入法人姓名'}; 
      if(!this.legal_phone)    return  {flag:false, ref: 'legal_phone', msg: '请输入法人联系方式'};
	    if(!this.$validator().isMobile(this.legal_phone))    return  {flag:false, ref: 'legal_phone', msg: '请输入正确的法人联系方式'};
      if(!this.legal_card_no)    return  {flag:false, ref: 'legal_card_no', msg: '请输入法人身份证号码'};
	    if(!this.$validator().isCard(this.legal_card_no))    return  {flag:false, ref: 'legal_card_no', msg: '请输入正确的法人身份证号码'};
      if(!this.control_phone)    return  {flag:false, ref: 'control_phone', msg: '请输入负责人联系方式'};
	    if(!this.$validator().isMobile(this.control_phone))    return  {flag:false, ref: 'control_phone', msg: '请输入正确的负责人联系方式'};
      if(!this.control_man)    return  {flag:false, ref: 'control_man', msg: '请输入负责人'}; 
      if(!this.license_img)    return  {flag:false, ref: 'license_img', msg: '请输入营业执照'}; 
      if(!this.legal_card_pros)    return  {flag:false, ref: 'legal_card_pros', msg: '请输入法人身份证正面'}; 
      if(!this.legal_card_cons)    return  {flag:false, ref: 'legal_card_cons', msg: '请输入法人身份证反面'}; 
      if(!this.postfee)    return  {flag:false, ref: 'postfee', msg: '请输入快递费'};


      let tanslines = [];
      let region_id = 0;
      this.circuit_list.forEach((v)=>{
          region_id = Array.isArray(v.arrive_region_id)?v.arrive_region_id.splice(-1)[0]:v.arrive_region_id;
          tanslines.push({tansline_id:v.tansline_id || "", arrive_region_id:region_id})
      })

      post_data = {
        buiss_time_start : this.deadline[0]/1000,    //去掉后面的毫秒级
        buiss_time_end  : this.deadline[1]/1000,
        company_id  : this.company_id,
        company_name  : this.company_name,
        organization_code : this.organization_code,
        company_address : this.company_address,
        legal_person  : this.legal_person,
        post_fee:this.postfee,
        legal_phone : this.legal_phone,
        legal_card_no : this.legal_card_no,
        control_phone : this.control_phone,
        control_man : this.control_man,
        license_img : this.license_img,
        legal_card_pros : this.legal_card_pros,
        legal_card_cons : this.legal_card_cons,
        tanslines:tanslines||[],
        type: this.company_type
      };

      return  {flag: true,  data: post_data,  msg: '获取成功'};
    }



  },
  methods: {
    // 加
    decrease() {
      this.circuit_list.push({
        line_id:0,
        start_line: "", // 起始点
        end_line: "" // 结束点
      });
    },
    // 减
    increase(index) {
      this.circuit_list.length > 1
        ? this.circuit_list.splice(index, 1)
        : this.$message.error("不能全部删除");
    },
    //上传营业执照成功
    licenseImgSuccess(res){
      console.log('console_success', res);
      this.license_img = res.data.url;
    },
    //上传身份证反面成功
    legalCardConsSuccess(res){
      this.legal_card_cons = res.data.url;
    },
    //上传身份证正面成功
    legalCardProsSuccess(res){
      this.legal_card_pros = res.data.url;
    },

    //获取所有到达站点
    arriveRegionList(){
        this.$raxios.post(this.$mapapi.arriveRegionList, {}).then(res=>{
        if(res.code==200){
         this.arrive_region = res.data || [{}];
        }else{
          this.$message.error(res.msg);
        }
      }).catch(res=>{

      })
    },

    //获取
    getData(){      //获取详情

          let post_data = {
          company_id: this.company_id
          };
       this.$raxios.post(this.$mapapi.getCompanyDetail, {data: post_data}).then(res=>{
         if(res.code==200){
          this.company_name = res.data.detail.company_name;
          this.organization_code = res.data.detail.organization_code;
          this.company_address = res.data.detail.company_address;
          this.buiss_time_start = res.data.detail.buiss_time_start * 1000;
          this.buiss_time_end = res.data.detail.buiss_time_end * 1000;
          this.deadline = [this.buiss_time_start, this.buiss_time_end];
          this.legal_person = res.data.detail.legal_person;
          this.legal_phone = res.data.detail.legal_phone;
          this.legal_card_no = res.data.detail.legal_card_no;
          this.control_man = res.data.detail.control_man;
          this.control_phone = res.data.detail.control_phone;
          this.license_img = res.data.detail.license_img+'1';
          this.legal_card_pros = res.data.detail.legal_card_pros;
          this.legal_card_cons = res.data.detail.legal_card_cons;
          this.company_type = res.data.detail.type;

      if(data.detail_tansline.length > 0){
        for( let val of data.detail_tansline){
          this.circuit_list.push({
            tansline_id : val.tansline_id, 
            depart_region_id : val.depart_region_id, 
            c_depart_region_id : val.c_depart_region_id, 
            p_depart_region_id : val.p_depart_region_id,
            arrive_region_id : val.arrive_region_id, 
            c_arrive_region_id : val.c_arrive_region_id, 
            p_arrive_region_id : val.p_arrive_region_id
          });
        }
      }else{
        this.circuit_list.push({});
      }
         }
      }).catch(res=>{
      this.$message.error(res.msg);
      })
    },

    submitInfo(){
      let res_data = this.postData;
      if(!res_data.flag){     //如果存在必填信息没有填的话，这里就报错、截断
        this.$message.error(res_data.msg);
        this.$refs[res_data.ref].focus();
        return;
      }
      this.$raxios.post(this.$mapapi.saveCompany, {data:res_data.data}).then(res=>{
        if(res.code==200){
        this.$message.success(res.msg);
        this.returnRoute();
        }else{
         this.$message.error(res.msg);
        }
      }).catch(res=>{

      })
    },
    returnRoute(){
      this.$router.push('/company_list');
    },
	  uploadLicenseImg(res){
		  this.uploadFiles(res,(file)=>{
			  this.license_img = file.data.url;
		  })
	  },
	  uploadLegalCardCons(res){
		  this.uploadFiles(res,(file)=>{
			  this.legal_card_cons = file.data.url;
		  })
	  },
	  uploadLegalCardPros(res){
		  this.uploadFiles(res,(file)=>{
			  this.legal_card_pros = file.data.url;
		  })
	  },
	  uploadFiles(params, callback){
		  let formData=new FormData();
		  formData.append("operator", 'goods');
		  formData.append("file", params.file);
		  this.$raxios.post(this.$mapapi.uploadFile, formData).then(res=>{
			  if(res.code==200){
				  this.$message.success(res.msg);
				  if(callback && typeof callback == 'function'){
					  callback(res)
				  }
			  }else{
				  this.$message.error(res.msg);
			  }
		  }).catch(res=>{

		  })
	  }

  },
  mounted() {
    this.company_id = this.$route.query.company_id;
    this.getData();
    this.arriveRegionList();

  },
  created() {
      console.log('$route.query.look',this.$route.query.look)
      this.look = this.$route.query.look;
  }
};
</script>

<style lang="less"  :scope="true"  src="@/style/company_manager/company_add.less">
</style>
